#include "CAppInstance.h"
#include "CServerList.h"
#include "resource.h"

#define IDM_OPEN		9010
#define IDM_SAVE		9011
#define IDM_PROPERTIES	9012

CAppInstance::CAppInstance(HINSTANCE hInstance, int nCmdShow)
{
	m_hInstance		= hInstance;
	m_nCmdShow		= nCmdShow;
	m_pTabCtrl		= NULL;
}

BOOL CAppInstance::Create(void)
{
	CServerList		ServerList					;
	LPSERVER		lpServ			=	NULL	;

	InitCommonControls();

	m_hWnd = CreateDialog(m_hInstance, MAKEINTRESOURCE(IDD_MAIN), 
		HWND_DESKTOP, CAppInstance::MainDlgProc);

	if(NULL != m_hWnd) {

		m_hToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | TBSTYLE_WRAPABLE, 
			0, 0, 0, 0, m_hWnd, NULL, m_hInstance, NULL);

		if(NULL != m_hToolBar) {
			m_hToolBarImages = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK, 3, 0);
			SendMessage(m_hToolBar, TB_SETIMAGELIST, 
				(WPARAM)0, (LPARAM)m_hToolBarImages);

			SendMessage(m_hToolBar, TB_LOADIMAGES, 
				(WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);

			TBBUTTON tbbuttons[3] = {
				{ MAKELONG(STD_FILEOPEN, 0)		, IDM_OPEN			, TBSTATE_ENABLED, BTNS_AUTOSIZE, {0}, 0, (INT_PTR)"Open"		},
				{ MAKELONG(STD_FILESAVE, 0)		, IDM_SAVE			, TBSTATE_ENABLED, BTNS_AUTOSIZE, {0}, 0, (INT_PTR)"Save"		},
				{ MAKELONG(STD_PROPERTIES, 0)	, IDM_PROPERTIES	, TBSTATE_ENABLED, BTNS_AUTOSIZE, {0}, 0, (INT_PTR)"Properties"	}
			} ;

			SendMessage(m_hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), (LPARAM)0);
			SendMessage(m_hToolBar, TB_ADDBUTTONS, (WPARAM)3, (LPARAM)&tbbuttons);
			SendMessage(m_hToolBar, TB_AUTOSIZE, 0, 0);
			ShowWindow(m_hToolBar, SW_SHOW);
		}

		while((lpServ = ServerList.GetNext(lpServ))) {
			SendMessage(GetDlgItem(m_hWnd, IDC_SERVERLIST), CB_ADDSTRING, 
				(WPARAM)0, (LPARAM)lpServ->lpszName);
		}

		SendMessage(GetDlgItem(m_hWnd, IDC_CHARSLOT), CB_ADDSTRING,
			(WPARAM)0, (LPARAM)"1\0");
		SendMessage(GetDlgItem(m_hWnd, IDC_CHARSLOT), CB_ADDSTRING,
			(WPARAM)0, (LPARAM)"2\0");

		SendMessage(GetDlgItem(m_hWnd, IDC_SERVERLIST), CB_SETCURSEL, (WPARAM)0, (LPARAM)0);
		SendMessage(GetDlgItem(m_hWnd, IDC_CHARSLOT), CB_SETCURSEL, (WPARAM)0, (LPARAM)0);

		SetWindowLong(m_hWnd, GWL_USERDATA, (LONG)this);
		ShowWindow(m_hWnd, m_nCmdShow);
		UpdateWindow(m_hWnd);

		m_pTabCtrl = new CTabCtrl(m_hInstance, GetDlgItem(m_hWnd, IDC_MAINTAB));
	}

	return  (NULL != m_hWnd && NULL != m_hToolBar)? TRUE : FALSE;
}

int CAppInstance::Run(void)
{
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0) > 0) {
		if(!IsDialogMessage(m_hWnd, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}

HWND CAppInstance::GetToolbarHandle(void)
{ return m_hToolBar; }


HINSTANCE CAppInstance::GetInstanceHandle(void)
{ return m_hInstance; }

CAppInstance::~CAppInstance()
{
	if(NULL != m_pTabCtrl)
		delete m_pTabCtrl;

}


////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CALLBACK CAppInstance::MainDlgProc(
					HWND	hwnd		,
					UINT	uMsg		,
					WPARAM	wParam		,
					LPARAM	lParam 
					)
{
	CAppInstance*		pInst			;
	RECT				rc				;
	POINT				pt				;

	pInst = (CAppInstance*)(GetWindowLong(hwnd, GWL_USERDATA));

	switch(uMsg) {
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_COMMAND:
		if(NULL != pInst) {
			if(HIWORD(wParam) == BN_CLICKED) {
				switch(LOWORD(wParam)) {
				case IDM_PROPERTIES:
					SendMessage(pInst->GetToolbarHandle(), 
						TB_GETITEMRECT, (WPARAM)2, (LPARAM)&rc)	;
					pt.x = rc.left ;
					pt.y = rc.top  ;
					ClientToScreen(pInst->GetToolbarHandle(), &pt);
					pt.y += rc.bottom - rc.top;
					TrackPopupMenu(GetSubMenu(LoadMenu(pInst->GetInstanceHandle(), MAKEINTRESOURCE(IDR_PROPERTIES)), 0), 
						TPM_LEFTALIGN | TPM_NONOTIFY, pt.x, pt.y, 0, hwnd, NULL);
					break;
				case IDM_SAVE:
					break;
				case IDM_OPEN:
					break;
				}
			}
		}
		break;
	default:
		return FALSE;
	}
	return TRUE;
}
